草庐IT

java - java中.this和.class的含义

全部标签

Javascript:Promises + this

这个问题在这里已经有了答案:Howdoesthe"this"keywordwork,andwhenshoulditbeused?(22个答案)关闭6年前。考虑以下代码:foo:function(){varself=this;varp1=p2=someFunctionThatReturnsAPromise();Promise.all([p1,p2]).then(self.bar);}bar:function(promises){varself=this;console.log(self);}输出:undefined但如果我改为执行以下操作:foo:function(){varself=t

JavaScript 'class' 和单例问题

我有一个使用另一个对象(不是单例)的单例对象,需要一些信息给服务器:varsingleton=(function(){/*_privateproperties*/varmyRequestManager=newRequestManager(params,//callbacksfunction(){previewRender(response);},function(){previewError();});/*_publicmethods*/return{/*makearequest*/previewRequest:function(request){myRequestManager.re

javascript - 箭头函数的 this 值

这个问题在这里已经有了答案:MethodsinES6objects:usingarrowfunctions(6个答案)Howdoesthe"this"keywordinJavascriptactwithinanobjectliteral?[duplicate](4个答案)关闭5年前。我正在尝试理解ECMAScript6中的箭头函数。这是我在阅读时遇到的定义:Arrowfunctionshaveimplicitthisbinding,whichmeansthatthevalueofthethisvalueinsideofanarrowfunctionisawaysthesameasthe

javascript - 在 Backbone.js 中,如何让模型父类(super class)默认值充当子类的默认值?

我有一个定义了一些默认值的类和一个定义了一些默认值的子类。但是当我创建子类的实例时,它只查看本地默认值,不会将其默认值与父类的默认值合并。有什么简单的方法可以做到这一点,而无需在每个子类的initialize函数中显式地将本地默认值与父默认值合并?varInventory=Backbone.Model.extend({defaults:{cat:3,dog:5}});varExtendedInventory=Inventory.extend({defaults:{rabbit:25}});varei=newExtendedInventory({});console.log(ei.att

javascript - ES6 箭头函数正在改变 Meteor.publish 中 this 的范围

这个问题在这里已经有了答案:Whatdoes"this"refertoinarrowfunctionsinES6?(10个答案)关闭7年前。所以我开始在Meteor中使用ES6,但显然如果你尝试使用带有箭头函数的Meteor.publish语法,this.userId是未定义的,而如果您将它与常规function(){}一起使用,this.userId可以完美运行,我假设是一种分配不同这,到userId但这只是一个猜测,有谁知道到底发生了什么?Meteor.startup(function(){Meteor.publish("Activities",function(){//withf

javascript - 在 javascript 的父闭包中引用 "this"

我想在Javascript中这样做:functionZ(f){f();}functionA(){this.b=function(){Z(function(){this.c()});}this.c=function(){alert('helloworld!');}}varfoo=newA();foo.b();可以这样实现:functionZ(f){f();}functionA(){varself=this;this.b=function(){Z(function(){self.c()});}this.c=function(){alert('helloworld!');}}varfoo=n

javascript - 为什么 `this===window` 给我假的?

我一直在测试以下代码,但Firefox16和Chrome22给出了不同的结果。console.log(this===window);//falseinFirefoxandtrueinChromeconsole.log(this.window===window);//trueinbothFirefoxandChrome(function(){console.log(this===window);//falseinFirefoxandtrueinChromeconsole.log(this.window===window);//trueinbothFirefoxandChrome})();

javascript - JQuery - 扩展和访问父类(super class)

在使用$.extend扩展对象时有没有办法访问super对象?我想扩展一个对象,覆盖一个方法,但是在子类方法中调用被覆盖的父类(superclass)方法。 最佳答案 不,因为没有父类(superclass)。根据jQuery.extend的文档:Description:Mergethecontentsoftwoormoreobjectstogetherintothefirstobject.为了调用“父类(superclass)”方法,您必须在某处保留“父类(superclass)”的副本(可能作为“后代”对象中的参数),并直接在“

javascript - 如何从 jQuery 函数访问外部 this?

出于好奇,有没有办法从paint函数访问this.color?functionFoo(color){this.color=color;this.paint=functionpaint(){$("select").each(function(idx,el){$(el).css("background",color);//OK//$(el).css("background",this.color);//this.colorisundefined})}}newFoo("red").paint();谢谢 最佳答案 varthat=this;

javascript - Javascript 的 Java 风格 Set 集合

我需要一个Set,其API类似于Java中的Set。这个实现:http://jsclass.jcoglan.com/set.html需要用到RequireJS,这下需要我的Java脑子绞尽脑汁了。发布一个作为Set功能的函数将是一个很好的答案。或已创建此代码的GoogleSet或其他科技巨头的链接。谷歌倒闭了怎么办?这个名字让我感到困惑,但它有一套。 最佳答案 在我看来,无论java.util.Set可以实现什么,都可以使用简单的javascript对象来完成。我不明白你为什么需要额外的库://emptysetvarbasket={